<?php

class RunstatisticHelper extends AppHelper {

	function projectname($id) {
		$result = mysql_query("SELECT p.name FROM runs r, projects p WHERE r.project_id = p.id AND r.id = '$id'");
		return mysql_fetch_object($result)->name;
	}
	
	function startdatum($id) {
		$result = mysql_query("SELECT DATE_FORMAT(date,'%d.%m.%Y') AS datum FROM runs r, runshasstatusruns rhs WHERE r.id = rhs.run_id AND rhs.status_id = '1' AND r.id = '$id'");	
		if(mysql_num_rows($result) > 0) {
			$date = mysql_fetch_object($result)->datum;
		}	
		else {
			$date = "-";
		}
		return $date;
	}
	
	function totalswaps($id) {
		$result = mysql_query("SELECT COUNT(*) AS count FROM runs r, runshasmacs rhm WHERE r.id = rhm.run_id AND r.id = '$id'");
		return mysql_fetch_object($result)->count;	
	}
	
	function blacklisted($id) {
		$result = mysql_query("SELECT COUNT(*) AS count FROM runs r,runshasmacs rhm, blacklists bl WHERE r.id = rhm.run_id AND rhm.mac_id = bl.mac_id AND r.id = '$id'");
		return mysql_fetch_object($result)->count;	
	}
	
	function exceptions($id) {
		$result = mysql_query("SELECT COUNT(*) AS count FROM runs r,runshasmacs rhm, blacklists bl WHERE r.id = rhm.run_id AND rhm.mac_id = bl.mac_id AND bl.problem_id = '10' AND r.id = '$id'");
		return mysql_fetch_object($result)->count;		
	}
	
	function ponds($id) {
		$result = mysql_query("SELECT COUNT(*) AS count FROM pondcases AS pc, partrequests AS pr, runshasmacs AS rhm WHERE pc.partrequest_id = pr.id AND pr.mac_id = rhm.mac_id AND rhm.run_id = '$id'");
		return mysql_fetch_object($result)->count;		
	}
	
	function pondsopen($id) {
		$result = mysql_query("SELECT COUNT(*) AS count FROM pondcases AS pc, partrequests AS pr, runshasmacs AS rhm WHERE pc.partrequest_id = pr.id AND pr.mac_id = rhm.mac_id AND pc.status_id IN (33,35) AND rhm.run_id = '$id'");
		return mysql_fetch_object($result)->count;		
	}
	
	
	function skipped($id) {
		$result = mysql_query("SELECT COUNT(*) AS count FROM macs AS m, runshasmacs AS rhm WHERE m.id = rhm.mac_id AND status_id = 27 AND rhm.run_id = '$id'");
		return mysql_fetch_object($result)->count;		
	}

	
	function prspending($id) {
		$result = mysql_query("SELECT COUNT(*) AS count FROM macs AS m, runshasmacs AS rhm WHERE m.id = rhm.mac_id AND status_id IN (23,24,26) AND rhm.run_id = '$id'");
		return mysql_fetch_object($result)->count;	
	}
	
	function prsclosed($id) {
		$result = mysql_query("SELECT COUNT(*) AS count FROM macs AS m, runshasmacs AS rhm WHERE m.id = rhm.mac_id AND status_id IN (25,27,45) AND rhm.run_id = '$id'");
		return mysql_fetch_object($result)->count;		
	}
	
	function batchstatus($id) {
		$result = mysql_query("SELECT DATE_FORMAT(date,'%d.%m.%Y') AS datum FROM runs r, runshasstatusruns rhs WHERE r.id = rhs.run_id AND rhs.status_id = '1' AND r.id = '$id'");	
		if(mysql_num_rows($result) == 0) {
			return "not started";
		}
		else {
			$result1 = mysql_query("SELECT COUNT(*) AS count FROM macs AS m, runshasmacs AS rhm WHERE m.id = rhm.mac_id AND status_id IN (25,27,45) AND rhm.run_id = '$id'");
			$completed = mysql_fetch_object($result1)->count;
			$result2 = mysql_query("SELECT COUNT(*) AS count FROM runs r, runshasmacs rhm WHERE r.id = rhm.run_id AND r.id = '$id'");
			$total = mysql_fetch_object($result2)->count;
			if($total == $completed) {
				return "completed";
			}
			else {
				return "running";
			}				
		}
	
	}
	
	
	
}

?>
